package com.learnyou.server.xingzuo.common.util;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class GsonUtils {

    private static Gson gson = new Gson();

    private GsonUtils() {

    }


    /**
     * javaBean、列表数组转换为json字符串
     */
    public static String obj2json(Object obj) {
        if (obj == null) {
            return StringUtils.EMPTY;
        }
        return gson.toJson(obj);
    }

    /**
     * javaBean转换为Map
     */
    public static Map obj2map(Object obj) {
        if (obj == null) {
            return new HashMap();
        }
        return json2map(obj2json(obj));
    }

    /**
     * json字符串转换为map
     *
     * @return
     */
    public static Map json2map(String jsonString) {
        if (StringUtils.isBlank(jsonString)) {
            return new HashMap();
        }
        return gson.fromJson(jsonString, HashMap.class);
    }
}
